package xiang.weiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xiang.weiyu.common.CustomException;
import xiang.weiyu.dto.SetmealDto;
import xiang.weiyu.mapper.SetmealMapper;
import xiang.weiyu.pojo.Setmeal;
import xiang.weiyu.pojo.SetmealDish;
import xiang.weiyu.service.SetmealDishService;
import xiang.weiyu.service.SetmealService;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐,同时保证套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDto setmealDto) {
        //1、保存套餐数据到 setmeal表中 ： SetmealMapper
        this.save(setmealDto);
        //2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
        //2.1 获取当前套餐的菜品列表
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        //2.3 获取套餐id，并set到setmealDish中
        Long id = setmealDto.getId();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(id);
        }
        //2.4 保存套餐菜品数据 setmealDish
        setmealDishService.saveBatch(list);
    }

    /**
     * 套餐删除/批量删除功能
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //1、判断当前要删除的套餐是否处于售卖状态，状态为1(起售中)，如果是，抛出异常(自定义的)
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        if (this.count(qw) > 0) {
            throw new CustomException("有套餐目前处于售卖状态,删除失败");
        }

        //2、删除套餐:
        this.removeByIds(ids);

        //3、删除套餐对应的菜品数据:
        LambdaQueryWrapper<SetmealDish> qwDish = new LambdaQueryWrapper<>();
        qwDish.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(qwDish);
    }

    /**
     * 套餐修改功能
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        //1、根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        this.updateById(setmealDto);

        //2、操作套餐对应的菜品数据： setmeal_dish
        //2.1 先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?
        // 获取套餐id   setmealDto.getId()
        Long setmealId = setmealDto.getId();

        // 使用 setmealDishMapper 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(qw);

        //2.2 重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        // setmealDto.getSetmealDishes() 获取套餐的菜品集合 List<SetmealDish>
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        // 遍历套餐菜品集合数据
        for (SetmealDish setmealDish : list) {
            // 有没有数据需要set ： 设置套餐id  setSetmealId
            setmealDish.setSetmealId(setmealId);
            // 添加套餐菜品数据 insert
            setmealDishService.save(setmealDish);
        }
    }
}
